<%= render partial: 'spree/admin/shared/product_tabs', locals: { current: :images } %>

<% content_for :page_actions do %>
  <%= yield :page_actions %>
  <%= product_preview_link(@product) %>
  <%= button_link_to(Spree.t(:new_image), spree.new_admin_product_image_url(@product), { class: "btn-success", icon: 'add.svg', id: 'new_image_link' }) if can? :create, Spree::Image %>
<% end %>

<% has_variants = @product.has_variants? %>

<% unless @product.variant_images.any? %>
  <div class="alert alert-warning">
    <%= Spree.t(:no_resource_found, resource: plural_resource_name(Spree::Image)) %>.
  </div>
<% else %>
<div class="table-responsive">
  <table class="table sortable" data-hook="images_table" data-sortable-link="<%= update_positions_admin_product_images_url(@product) %>">
    <thead>
      <tr data-hook="images_header">
        <th colspan="2">
          <%= Spree.t(:thumbnail) %>
        </th>
        <% if has_variants %>
          <th>
            <%= Spree::Variant.model_name.human %>
          </th>
        <% end %>
        <th>
          <%= Spree.t(:alt_text) %>
        </th>
        <th class="actions"></th>
      </tr>
    </thead>

    <tbody id="sortVert">
      <% (@product.variant_images).each do |image| %>
        <tr id="<%= spree_dom_id image %>" data-hook="images_row">
          <td class="move-handle text-center">
            <% if can? :edit, image %>
              <%= svg_icon name: "sort.svg", width: '18', height: '18' %>
            <% end %>
          </td>
          <td class="image">
            <%= link_to image_tag(main_app.url_for(image.url(:mini))), main_app.rails_blob_url(image.attachment) %>
          </td>
          <% if has_variants %>
            <td><%= options_text_for(image) %></td>
          <% end %>
          <td><%= image.alt %></td>
          <td class="actions actions-2 text-right">
            <%= link_to_with_icon('edit.svg', Spree.t(:edit), spree.edit_admin_product_image_url(@product, image), class: 'btn btn-primary btn-sm', no_text: true, data: { action: 'edit' }) if can? :edit, image %>
            <%= link_to_delete(image, { url: spree.admin_product_image_url(@product, image), no_text: true }) if can? :destroy, image %>
          </td>
        </tr>
      <% end %>
    </tbody>
  </table>
</div>
<% end %>
